import Vue from 'vue'
import VueRouter from 'vue-router'
import LoginView from "@/components/LoginView.vue";
import HomeViewPC from "@/components/HomeViewPC.vue";
import AcountListDoctor from "@/components/views/account/AcountListDoctor.vue";
import TaskGropList from "@/components/views/task/TaskGropList.vue";
import PubTask from "@/components/views/task/PubTask.vue";
import AccLogin from "@/components/views/answer/AccLogin.vue";
import QuestionList from "@/components/views/answer/QuestionList.vue"
import QeustionHint from "@/components/views/answer/QeustionHint.vue";
import StartTest from "@/components/views/answer/StartTest.vue";
import ResultList from "@/components/views/result/ResultList.vue";
import ResultInfo from "@/components/views/result/ResultInfo.vue"
import UploadQue from "@/components/util/UploadQue.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: LoginView
  },
  {
    path: '/',
    redirect: '/login'
  },
  {
    path:'/acc_login',
    component: AccLogin
  },{
    path:'/que_list',
    component: QuestionList
  },{
    path:'/que_hint',
    component: QeustionHint
  },
  {
    path:'/start_test',
    component: StartTest
  },
  {
    path:'/home',
    component: HomeViewPC,
    children: [
      {path:'/account_list', component: AcountListDoctor, name:'用户列表'},
      {path:'/task_list',component: TaskGropList,name:'任务列表'},
      {path:'/task_pub',component: PubTask,name:'发布任务'},
      {path:'/res_list',component: ResultList,name:'评测结果'},
      {path:'/res_info',component: ResultInfo,name:'报告详情'},
      {path:'/upload_que',component: UploadQue,name:'文件上传'},
    ]
  }
]

const router = new VueRouter({
  routes
})

// next()即为放行，next(‘/login’)即为跳转
router.beforeEach((to,from,next) =>{
  //访问登陆页面直接放行
  if(to.path=='/login' || to.path=="/acc_login"){
    return next();
  }else{
    //从session中获取管理员token，进行验证，通过后放行
    let token = sessionStorage.getItem("token");
    if(!token){
        return next("/login"); //注意此地址是否存在
    }else {
      return next();
    }
  }
})

//处理重复跳转报错的问题
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

export default router
